package com.ilop.sthome.ui.activity.device;

import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.base.OnCallBackToRefresh;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.mvp.contract.mine.RoomContract;
import com.ilop.sthome.mvp.present.mine.RoomPresenter;
import com.ilop.sthome.ui.adapter.room.RoomRecommendAdapter;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityRoomEditorBinding;

/**
 * @author skygge
 * @Date on 2020-02-11.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：房间编辑页面
 */
public class RoomEditorActivity extends ContainLoginActivity<RoomPresenter, ActivityRoomEditorBinding> implements RoomContract.IView {

    private int mPosition;
    private boolean mIsEdit;
    private String mRoomName;
    private String[] mRoomList;
    private GridLayoutManager mManager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_editor;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new RoomPresenter(mContext);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mPosition = getIntent().getIntExtra(CommonId.KEY_BIND_ROOM, 0);
        mIsEdit = getIntent().getBooleanExtra(CommonId.KEY_MODIFY, false);
        mRoomList = new String[]{mContext.getString(R.string.living_room), mContext.getString(R.string.bedroom),
                mContext.getString(R.string.master_bedroom), mContext.getString(R.string.supine_bedroom),
                mContext.getString(R.string.kitchen), mContext.getString(R.string.study),
                mContext.getString(R.string.stairs), mContext.getString(R.string.toilet)};
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etRoomName);
        mDBind.btDeleteRoom.setVisibility(mIsEdit ? View.VISIBLE : View.GONE);
        RoomRecommendAdapter mAdapter = new RoomRecommendAdapter(mContext, mRoomList);
        mManager = new GridLayoutManager(mContext, 4);
        mManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return ConfigureUtil.gridLayoutLineNum(mRoomList[i].length());
            }
        });
        mDBind.rvRoomRecommend.setLayoutManager(mManager);
        mDBind.rvRoomRecommend.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        if (mIsEdit){
            mPresent.getRoomInfo(mPosition);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        LiveDataBus.get().with(CommonId.KEY_RECOMMEND_ID, String.class).observe(this, s -> mDBind.etRoomName.setText(s));
        mDBind.tvSaveRoom.setOnClickListener(v -> {
            hideSoftKeyboard();
            String roomName = mDBind.etRoomName.getText().toString();
            if (!TextUtils.isEmpty(roomName)){
                onCheckName(roomName);
            }else {
                showToast(getString(R.string.please_input_name));
            }
        });
        mDBind.btDeleteRoom.setOnClickListener(v -> mPresent.onDeleteRoom());
        mDBind.ivRoomBack.setOnClickListener(v -> {
            hideSoftKeyboard();
            String roomName = mDBind.etRoomName.getText().toString();
            if (!TextUtils.isEmpty(roomName) && !roomName.equals(mRoomName)){
                whetherToSave(roomName);
            }else {
                finish();
            }
        });
    }

    private void onCheckName(String roomName){
        if (mIsEdit){
            mPresent.onUpdateRoom(roomName);
        }else {
            String mRoomId = RoomDaoUtil.getInstance().findFirstEmptyRoom();
            if (mRoomId == null){
                mDBind.configTips.setText(getString(R.string.room_max));
            }else {
                RoomBean mRoom = RoomDaoUtil.getInstance().findRoomByRoomName(roomName);
                if (mRoom!=null){
                    showToast(getString(R.string.room_already_exists));//房间已存在
                }else {
                    mPresent.onSaveRoom(mRoomId, roomName);
                }
            }
        }
    }

    private void whetherToSave(String name){
        BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
            @Override
            public void onConfirm() {
                onCheckName(name);
            }

            @Override
            public void onCancel() {
                finish();
            }
        });
        mDialog.setMsg(mContext.getString(R.string.save_or_not));
        mDialog.show();
    }

    @Override
    public void showRoomName(String name) {
        mRoomName = name;
        mDBind.etRoomName.setText(name);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void doSuccess(String msg) {
        showToast(msg);
        finish();
    }

    @Override
    public void onBackPressed() {
        String roomName = mDBind.etRoomName.getText().toString();
        if (!TextUtils.isEmpty(roomName) && !roomName.equals(mRoomName)){
            whetherToSave(roomName);
        }else {
            finish();
        }
    }
}
